package com.zhkj.txg.module.home.ui

import android.content.Context
import android.content.Intent
import android.view.View
import android.widget.ImageView
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.entity.node.BaseNode
import com.zhkj.lib.base.ViewModelActivity
import com.zhkj.lib.config.BASE_URL
import com.zhkj.lib.http.HttpResponseStatus
import com.zhkj.txg.R
import com.zhkj.txg.base.CustomApplication
import com.zhkj.txg.module.home.adapter.*
import com.zhkj.txg.module.home.entity.CategoryTopEntity
import com.zhkj.txg.module.home.vm.CategoryViewModel
import com.zhkj.txg.module.order.ui.OrderDetailActivity
import com.zhkj.txg.utils.ImageUtils
import com.zhkj.txg.utils.typeJump
import kotlinx.android.synthetic.main.activity_category.*
import kotlinx.android.synthetic.main.activity_category.rvCateLeft
import kotlinx.android.synthetic.main.activity_category.rvCateRight
import kotlinx.android.synthetic.main.activity_category.titleCategoryAll
import kotlinx.android.synthetic.main.fragment_category_2.*

@Suppress("UNCHECKED_CAST")
class CategoryActivity : ViewModelActivity<CategoryViewModel>() {

    companion object {
        private val PARAMS = "params"

        fun open(context: Context, params: Int = 0) {
            val intent = Intent(context, CategoryActivity::class.java)
                .putExtra(PARAMS, params)
            context.startActivity(intent)
        }
    }

    private val categoryLeftAdapter: CategoryLeftAdapter by lazy {
        CategoryLeftAdapter(intent.getIntExtra(PARAMS, 0)) {
            categoryRightAdapter.setNewData(it.child)
            ImageUtils.loadImage(
                this, imageCategoryRightHeard,
                if (it.image.startsWith("http")) it.image else BASE_URL + it.image
            )
        }
    }
    private val categoryRightAdapter = CategoryRightAdapter()

    override fun setViewModel(): CategoryViewModel = createViewModel()
    override fun statusTitleBar(): View? = titleCategoryAll
    override val layoutResId: Int = R.layout.activity_category


    override fun initUI() {
        // 设置TitleBar的高度
        titleCategoryAll.setFragmentStatusBar()

        with(rvCateLeft) {
            layoutManager = LinearLayoutManager(context)
            adapter = categoryLeftAdapter
        }
        with(rvCateRight) {
            layoutManager = GridLayoutManager(context, 3)
            adapter = categoryRightAdapter
        }

        categoryRightAdapter.setHeaderView(createHeaderView())
        categoryRightAdapter.setOnItemClickListener { adapter, view, position ->
            ProductListActivity.categoryOpen(
                this, categoryRightAdapter.data[position].name, categoryRightAdapter.data[position].id,
                CustomApplication.getShopId()
            )
        }
    }

    override fun initData() {

        viewModel.shopCate(CustomApplication.getShopId()).takeIf {
            !it.hasObservers()
        }?.observe(this, Observer { response ->
            handlerResponseStatus(response.status, response.statusTip)
            if (response.status == HttpResponseStatus.Status.SUCCESS) response.response?.let {
                categoryLeftAdapter.setNewData(it.data)
            }
        })
    }

    private lateinit var imageCategoryRightHeard: ImageView

    private fun createHeaderView(): View {
        return layoutInflater.inflate(R.layout.item_category_header, null).apply {
            imageCategoryRightHeard = findViewById(R.id.imageCategoryRightHeard)
        }
    }
}